<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Butterfly 安裝文檔(六) 進階教程 | HJ BLOG</title><meta name="keywords" content="hexo,butterfly,主題,doc,教程,文檔"><meta name="author" content="娄豪杰"><meta name="copyright" content="娄豪杰"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Butterfly安裝文檔-進階教程">
<meta property="og:type" content="article">
<meta property="og:title" content="Butterfly 安裝文檔(六) 進階教程">
<meta property="og:url" content="http://example.com/article/4073eda.html">
<meta property="og:site_name" content="HJ BLOG">
<meta property="og:description" content="Butterfly安裝文檔-進階教程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-06-cover.png">
<meta property="article:published_time" content="2019-01-06T14:39:25.000Z">
<meta property="article:modified_time" content="2021-07-27T01:41:38.000Z">
<meta property="article:author" content="娄豪杰">
<meta property="article:tag" content="教程">
<meta property="article:tag" content="Hexo">
<meta property="article:tag" content="主題">
<meta property="article:tag" content="butterfly">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-06-cover.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/article/4073eda"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Butterfly 安裝文檔(六) 進階教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-07-27 09:41:38'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/static-butterfly/dist/css/index.min.css"><link rel="stylesheet" href="/css/custom.css"><meta name="generator" content="Hexo 5.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-06-cover.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">HJ BLOG</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Butterfly 安裝文檔(六) 進階教程</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-01-06T14:39:25.000Z" title="发表于 2019-01-06 22:39:25">2019-01-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-07-27T01:41:38.000Z" title="更新于 2021-07-27 09:41:38">2021-07-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Docs%E6%96%87%E6%AA%94/">Docs文檔</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Butterfly 安裝文檔(六) 進階教程"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><div class="note blue icon flat"><i class="note-icon fas fa-bullhorn"></i><p>📖 本教程更新於 2021 年 07 月 20 日，教程的內容針對最新<strong>穩定版</strong>而更新（如果你是舊版，教程會有些出入，請留意）</p>
<p>🦋 Butterfly 已經更新到 <a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly/releases/tag/3.8.2">3.8.2</a></p>
</div>

<div class="note green icon flat"><i class="note-icon fas fa-rocket"></i><p>📚 文檔目錄</p>
<p><a href="#">Post not found: Butterfly-安裝文檔-一-快速開始 🚀 快速開始</a> - <a href="#">Post not found: Butterfly-安裝文檔-二-主題頁面 📑 主題頁面</a> - <a href="#">Post not found: Butterfly-安裝文檔-三-主題配置-1 🛠 主題配置-1</a> - <a href="#">Post not found: Butterfly-安裝文檔-四-主題配置-2 ⚔️ 主題配置-2</a> - <a href="#">Post not found: Butterfly-安裝文檔-五-主題問答 ❓ 主題問答</a> - <a href="#">Post not found: Butterfly-安裝文檔-六-進階教程 ⚡️ 進階教程</a> - <a href="#">Post not found: Butterfly-安裝文檔-七-更新日誌 ✨ 更新日誌</a> - <a href="#">Post not found: Butterfly-打賞 🤞 打賞</a></p>
</div>

<div class="note orange icon flat"><i class="note-icon fas fa-magic"></i><p>你可以通過右下角的 <strong>簡</strong> 按鈕切換為簡體顯示</p>
</div>

<hr>
<h2 id="建議"><a href="#建議" class="headerlink" title="建議"></a>建議</h2><ol>
<li>不要把個人需要的文件/圖片放在主題<code>source</code>文件夾裏，因為在升級主題的過程中，可能會把文件覆蓋刪除了。<br>在 Hexo 根目錄的<code>source</code>文件夾裏，創建一個文件夾來放置個人文件/圖片。<br>引用文件直接為<code>/文件夾名稱/文件名</code></li>
</ol>
<h2 id="音樂"><a href="#音樂" class="headerlink" title="音樂"></a>音樂</h2><p>音樂界面使用了插件 <code>hexo-tag-aplayer</code><br>使用方法請參考插件<a target="_blank" rel="noopener" href="https://github.com/MoePlayer/hexo-tag-aplayer/blob/master/docs/README-zh_cn.md">文檔</a></p>
<p>音樂頁面只是普通的 page 頁，按普通頁面操作生成就行。</p>
<blockquote>
<p>以下內容可供<strong>選擇</strong>配置</p>
<p>注意： 仍需要安裝插件<a target="_blank" rel="noopener" href="https://github.com/MoePlayer/hexo-tag-aplayer">hexo-tag-aplayer</a></p>
</blockquote>
<p>插件會在每一個文件都插入 js 和 css，為了避免這一情況，3.0 版本內置了 aplayer 需要的 css 和 js。</p>
<p>首先在 Hexo 根目錄<code>_config</code>裏配置<code>asset_inject</code>為<code>false</code></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">aplayer:</span></span><br><span class="line">  <span class="attr">asset_inject:</span> <span class="literal">false</span></span><br></pre></td></tr></table></figure>
<p>然後在你需要使用 aplayer 的頁面 Front-matter 添加</p>
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aplayer: true</span><br></pre></td></tr></table></figure>
<p>這樣只會在需要 aplayer 的頁面插入 js 和 css。</p>
<p>如何添加全局 Aplayer 播放，請參考 <a href="/posts/507c070f/">這篇文章</a></p>
<h2 id="電影"><a href="#電影" class="headerlink" title="電影"></a>電影</h2><p>電影界面使用了插件 <code>hexo-butterfly-douban</code><br>使用方法請參考插件的<a target="_blank" rel="noopener" href="https://github.com/jerryc127/butterfly-plugins/tree/main/hexo-butterfly-douban">文檔</a>。</p>
<p><strong>注意：</strong></p>
<ol>
<li>hexo-butterfly-douban 會主動生成頁面，所以不需要自己創建。</li>
<li>如遇到無法抓取問題，顯示 <code>INFO 0 movies have been loaded in xxx ms, because you are offline or your network is bad</code><br>請過段時間再試試，這我也無能為力。</li>
</ol>
<h2 id="説説"><a href="#説説" class="headerlink" title="説説"></a>説説</h2><h3 id="Artitalk"><a href="#Artitalk" class="headerlink" title="Artitalk"></a>Artitalk</h3><p>安裝插件 <a target="_blank" rel="noopener" href="https://www.npmjs.com/package/hexo-butterfly-artitalk">hexo-butterfly-artitalk</a></p>
<p>具體配置查看<a target="_blank" rel="noopener" href="https://github.com/jerryc127/butterfly-plugins/tree/main/hexo-butterfly-artitalk">插件文檔</a></p>
<h3 id="HexoPlusPlus-Talk"><a href="#HexoPlusPlus-Talk" class="headerlink" title="HexoPlusPlus Talk"></a>HexoPlusPlus Talk</h3><p>安裝插件 <a target="_blank" rel="noopener" href="https://www.npmjs.com/package/hexo-butterfly-hpptalk">hexo-butterfly-hpptalk</a></p>
<p>具體配置查看<a target="_blank" rel="noopener" href="https://github.com/jerryc127/butterfly-plugins/tree/main/hexo-butterfly-hpptalk">插件文檔</a></p>
<h2 id="自定義代碼配色"><a href="#自定義代碼配色" class="headerlink" title="自定義代碼配色"></a>自定義代碼配色</h2><a class="btn-beautify button--animated block blue" href="/posts/b37b5fe3/" 
  title="點擊前往"><i class="fas fa-code"></i><span>點擊前往</span></a>

<h2 id="自定義側邊欄"><a href="#自定義側邊欄" class="headerlink" title="自定義側邊欄"></a>自定義側邊欄</h2><a class="btn-beautify button--animated block green" href="/posts/ea33ab97/" 
  title="點擊前往"><i class="fas fa-lightbulb"></i><span>點擊前往</span></a>

<h2 id="添加全局吸底-Aplayer-教程"><a href="#添加全局吸底-Aplayer-教程" class="headerlink" title="添加全局吸底 Aplayer 教程"></a>添加全局吸底 Aplayer 教程</h2><a class="btn-beautify button--animated block pink" href="/posts/507c070f/" 
  title="點擊前往"><i class="fas fa-music"></i><span>點擊前往</span></a>

<h2 id="jQuery-加載"><a href="#jQuery-加載" class="headerlink" title="jQuery 加載"></a>jQuery 加載</h2><p>主題已於 <code>3.4.0</code> 去除 jQuery 的引用，但是部分功能仍需要加載 jQuery（<em>Justified Gallery</em>和 <em>fancybox</em>)。</p>
<p>如果你仍需要使用 jQuery，可以調用主題提供的 function，防止 jQuery 多次加載。</p>
<p><code>btf.isJqueryLoad(fn)</code></p>
<p>function 會判斷是否加載了 jQuery ，如果沒有，加載 jQuery 後運行 fn。如果有，直接運行 fn。</p>
<p>使用方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 方法 1</span></span><br><span class="line">btf.isJqueryLoad(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    你的<span class="function"><span class="keyword">function</span></span></span><br><span class="line"><span class="function">&#125;&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">// 方法 2</span></span><br><span class="line"><span class="function"><span class="function"><span class="keyword">function</span> <span class="title">myFn</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="function">    你的<span class="function"><span class="keyword">function</span></span></span></span><br><span class="line"><span class="function"><span class="function">&#125;</span></span></span><br><span class="line"><span class="function"><span class="function"><span class="title">btf</span>.<span class="title">isJqueryLoad</span>(<span class="params">myFn</span>)</span></span></span><br></pre></td></tr></table></figure>
<h2 id="Gulp-壓縮"><a href="#Gulp-壓縮" class="headerlink" title="Gulp 壓縮"></a>Gulp 壓縮</h2><p><a target="_blank" rel="noopener" href="https://www.gulpjs.com.cn/">Gulp</a> 是一款自動化構建的工具，擁有眾多的插件。而我們只需要使用到幾個插件來壓縮 Html/css/js。</p>
<h3 id="安裝-Gulp"><a href="#安裝-Gulp" class="headerlink" title="安裝 Gulp"></a>安裝 Gulp</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -<span class="literal">-global</span> gulp<span class="literal">-cli</span></span><br></pre></td></tr></table></figure>
<h3 id="插件安裝"><a href="#插件安裝" class="headerlink" title="插件安裝"></a>插件安裝</h3><h4 id="壓縮-HTML"><a href="#壓縮-HTML" class="headerlink" title="壓縮 HTML"></a>壓縮 HTML</h4><p>可以使用<a target="_blank" rel="noopener" href="https://github.com/anseki/gulp-htmlclean">gulp-htmlclean</a>和<a target="_blank" rel="noopener" href="https://github.com/pioug/gulp-html-minifier-terser">gulp-html-minifier-terser</a>來壓縮 HTML</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install gulp<span class="literal">-htmlclean</span> -<span class="literal">-save</span><span class="literal">-dev</span></span><br><span class="line">npm install -<span class="literal">-save</span> gulp<span class="literal">-html</span><span class="literal">-minifier</span><span class="literal">-terser</span></span><br></pre></td></tr></table></figure>
<h4 id="壓縮-CSS"><a href="#壓縮-CSS" class="headerlink" title="壓縮 CSS"></a>壓縮 CSS</h4><p>可以使用<a target="_blank" rel="noopener" href="https://github.com/scniro/gulp-clean-css">gulp-clean-css</a>來壓縮 CSS</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install gulp<span class="literal">-clean</span><span class="literal">-css</span> -<span class="literal">-save</span><span class="literal">-dev</span></span><br></pre></td></tr></table></figure>
<h4 id="壓縮-JS"><a href="#壓縮-JS" class="headerlink" title="壓縮 JS"></a>壓縮 JS</h4><p>由於 Butterfly 主題中的 JS 使用到了部分 ES6 語法，因此不能只使用 <a target="_blank" rel="noopener" href="https://github.com/terinjokes/gulp-uglify">gulp-uglify </a>來壓縮，還需要搭配其它的插件。兩種方法都可以有效的壓縮 JS 代碼，選一種適合自己的就行。</p>
<p><a target="_blank" rel="noopener" href="https://github.com/duan602728596/gulp-terser">gulp-terser</a> 是直接壓縮 js 代碼，不會進行轉換</p>
<p><a target="_blank" rel="noopener" href="https://github.com/babel/gulp-babel">gulp-babel</a>是一個 JavaScript 轉換編譯器，可以把 ES6 轉換成 ES5</p>
<p><code>gulp-terser</code></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install gulp<span class="literal">-terser</span> -<span class="literal">-save</span><span class="literal">-dev</span></span><br></pre></td></tr></table></figure>
<p><code>gulp-uglify + gulp-babel</code></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install -<span class="literal">-save</span><span class="literal">-dev</span> gulp<span class="literal">-uglify</span></span><br><span class="line">npm install -<span class="literal">-save</span><span class="literal">-dev</span> gulp<span class="literal">-babel</span> @babel/core @babel/p<span class="built_in">reset-env</span></span><br></pre></td></tr></table></figure>
<h4 id="壓縮圖片"><a href="#壓縮圖片" class="headerlink" title="壓縮圖片"></a>壓縮圖片</h4><p>可以使用<a target="_blank" rel="noopener" href="https://github.com/sindresorhus/gulp-imagemin">gulp-imagemin</a>來壓縮圖片</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -<span class="literal">-save</span><span class="literal">-dev</span> gulp<span class="literal">-imagemin</span></span><br></pre></td></tr></table></figure>
<h3 id="創建-gulpfile-文件"><a href="#創建-gulpfile-文件" class="headerlink" title="創建 gulpfile 文件"></a>創建 gulpfile 文件</h3><p>在 Hexo 的根目錄，創建一個<code> gulpfile.js</code>文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> gulp = <span class="built_in">require</span>(<span class="string">&quot;gulp&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> cleanCSS = <span class="built_in">require</span>(<span class="string">&quot;gulp-clean-css&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> htmlmin = <span class="built_in">require</span>(<span class="string">&quot;gulp-html-minifier-terser&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> htmlclean = <span class="built_in">require</span>(<span class="string">&quot;gulp-htmlclean&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> imagemin = <span class="built_in">require</span>(<span class="string">&quot;gulp-imagemin&quot;</span>);</span><br><span class="line"><span class="comment">// gulp-tester (如果使用 gulp-tester,把下面的//去掉)</span></span><br><span class="line"><span class="comment">// const terser = require(&#x27;gulp-terser&#x27;);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// babel (如果不是使用bebel,把下面兩行註釋掉)</span></span><br><span class="line"><span class="keyword">const</span> uglify = <span class="built_in">require</span>(<span class="string">&quot;gulp-uglify&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> babel = <span class="built_in">require</span>(<span class="string">&quot;gulp-babel&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// minify js - babel（ 如果不是使用bebel,把下面註釋掉）</span></span><br><span class="line">gulp.task(<span class="string">&quot;compress&quot;</span>, <span class="function">() =&gt;</span></span><br><span class="line">  gulp</span><br><span class="line">    .src([<span class="string">&quot;./public/**/*.js&quot;</span>, <span class="string">&quot;!./public/**/*.min.js&quot;</span>])</span><br><span class="line">    .pipe(</span><br><span class="line">      babel(&#123;</span><br><span class="line">        presets: [<span class="string">&quot;@babel/preset-env&quot;</span>],</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(</span><br><span class="line">      uglify().on(<span class="string">&quot;error&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(e);</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public&quot;</span>))</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// minify js - gulp-tester (如果使用 gulp-tester,把下面前面的//去掉)</span></span><br><span class="line"><span class="comment">// gulp.task(&#x27;compress&#x27;, () =&gt;</span></span><br><span class="line"><span class="comment">//   gulp.src([&#x27;./public/**/*.js&#x27;, &#x27;!./public/**/*.min.js&#x27;])</span></span><br><span class="line"><span class="comment">//     .pipe(terser())</span></span><br><span class="line"><span class="comment">//     .pipe(gulp.dest(&#x27;./public&#x27;))</span></span><br><span class="line"><span class="comment">// )</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// css</span></span><br><span class="line">gulp.task(<span class="string">&quot;minify-css&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> gulp</span><br><span class="line">    .src(<span class="string">&quot;./public/**/*.css&quot;</span>)</span><br><span class="line">    .pipe(cleanCSS())</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public&quot;</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 壓縮 public 目錄內 html</span></span><br><span class="line">gulp.task(<span class="string">&quot;minify-html&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> gulp</span><br><span class="line">    .src(<span class="string">&quot;./public/**/*.html&quot;</span>)</span><br><span class="line">    .pipe(htmlclean())</span><br><span class="line">    .pipe(</span><br><span class="line">      htmlmin(&#123;</span><br><span class="line">        removeComments: <span class="literal">true</span>, <span class="comment">// 清除 HTML 註釋</span></span><br><span class="line">        collapseWhitespace: <span class="literal">true</span>, <span class="comment">// 壓縮 HTML</span></span><br><span class="line">        collapseBooleanAttributes: <span class="literal">true</span>, <span class="comment">// 省略布爾屬性的值 &lt;input checked=&quot;true&quot;/&gt; ==&gt; &lt;input /&gt;</span></span><br><span class="line">        removeEmptyAttributes: <span class="literal">true</span>, <span class="comment">// 刪除所有空格作屬性值 &lt;input id=&quot;&quot; /&gt; ==&gt; &lt;input /&gt;</span></span><br><span class="line">        removeScriptTypeAttributes: <span class="literal">true</span>, <span class="comment">// 刪除 &lt;script&gt; 的 type=&quot;text/javascript&quot;</span></span><br><span class="line">        removeStyleLinkTypeAttributes: <span class="literal">true</span>, <span class="comment">// 刪除 &lt;style&gt; 和 &lt;link&gt; 的 type=&quot;text/css&quot;</span></span><br><span class="line">        minifyJS: <span class="literal">true</span>, <span class="comment">// 壓縮頁面 JS</span></span><br><span class="line">        minifyCSS: <span class="literal">true</span>, <span class="comment">// 壓縮頁面 CSS</span></span><br><span class="line">        minifyURLs: <span class="literal">true</span>,</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public&quot;</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 壓縮 public/uploads 目錄內圖片</span></span><br><span class="line">gulp.task(<span class="string">&quot;minify-images&quot;</span>, <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">  gulp</span><br><span class="line">    .src(<span class="string">&quot;./public/img/**/*.*&quot;</span>)</span><br><span class="line">    .pipe(</span><br><span class="line">      imagemin(&#123;</span><br><span class="line">        optimizationLevel: <span class="number">5</span>, <span class="comment">// 類型：Number  預設：3  取值範圍：0-7（優化等級）</span></span><br><span class="line">        progressive: <span class="literal">true</span>, <span class="comment">// 類型：Boolean 預設：false 無失真壓縮jpg圖片</span></span><br><span class="line">        interlaced: <span class="literal">false</span>, <span class="comment">// 類型：Boolean 預設：false 隔行掃描gif進行渲染</span></span><br><span class="line">        multipass: <span class="literal">false</span>, <span class="comment">// 類型：Boolean 預設：false 多次優化svg直到完全優化</span></span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public/img&quot;</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 執行 gulp 命令時執行的任務</span></span><br><span class="line">gulp.task(</span><br><span class="line">  <span class="string">&quot;default&quot;</span>,</span><br><span class="line">  gulp.parallel(<span class="string">&quot;compress&quot;</span>, <span class="string">&quot;minify-css&quot;</span>, <span class="string">&quot;minify-html&quot;</span>, <span class="string">&quot;minify-images&quot;</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="運行"><a href="#運行" class="headerlink" title="運行"></a>運行</h3><p>在<code>hexo g</code>之後運行<code>gulp</code>就行。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gulp</span><br></pre></td></tr></table></figure>
<h2 id="PWA"><a href="#PWA" class="headerlink" title="PWA"></a>PWA</h2><div class="note warning flat"><p>這是另一種實現 PWA 的方法，使用這個方法之前，請先<strong>卸載</strong>掉其它的 PWA 插件。</p>
</div>

<div class="note info flat"><p>這部分文章參考<a target="_blank" rel="noopener" href="https://io-oi.me/tech/pwa-via-workbox/">利用 Workbox 實現博客的 PWA</a></p>
</div>

<p>據維基百科介紹</p>
<blockquote>
<p>漸進式網絡應用程式（英語：<strong>P</strong>rogressive <strong>W</strong>eb <strong>A</strong>pps，簡稱：<strong>PWA</strong>）是一種普通<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E7%B6%B2%E9%A0%81">網頁</a>或<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E7%B6%B2%E7%AB%99">網站</a>架構起來的<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E7%B6%B2%E7%B5%A1%E6%87%89%E7%94%A8%E7%A8%8B%E5%BA%8F">網絡應用程式</a>，但它可以以傳統<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E6%87%89%E7%94%A8%E7%A8%8B%E5%BA%8F">應用程式</a>或原生<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E7%A7%BB%E5%8B%95%E6%87%89%E7%94%A8%E7%A8%8B%E5%BA%8F">移動應用程式</a>形式展示給用户。這種應用程式形態視圖將目前最為現代化的<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/%E7%80%8F%E8%A6%BD%E5%99%A8">瀏覽器</a>提供的功能與行動裝置的體驗優勢相結合。</p>
</blockquote>
<p>當你的網站實現了 PWA，那就代表了</p>
<ol>
<li>用户可以添加你的博客到電腦 ╱ 手機的桌面，以原生應用般的方式瀏覽你的博客</li>
<li>用户可以更快速地瀏覽你的博客</li>
<li>用户可以離線瀏覽你的博客</li>
</ol>
<p>Hexo 已經有很多插件可以實現 PWA，下面是另一種實現方法，需要有 Gulp 就行。這種方法也可實現彈窗提醒用户刷新網站（當網站有更新時）</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-pwa-notice.png" alt="image-20200528222826867"></p>
<p>此方法是使用 <a target="_blank" rel="noopener" href="https://developers.google.com/web/fundamentals/primers/service-workers/">Service Worker</a>。我們使用 <a target="_blank" rel="noopener" href="https://developers.google.com/web/tools/workbox/">Workbox</a> 這個工具生成 <code>sw.js</code> 以快速實現 Service Worker ，並實現頁面的預緩存和頁面更新後的提醒功能。</p>
<h3 id="開啟設置和配置-manifest-json"><a href="#開啟設置和配置-manifest-json" class="headerlink" title="開啟設置和配置 manifest.json"></a>開啟設置和配置 manifest.json</h3><p>在<code>主題配置文件中</code>中開啟 pwa 選項</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">pwa:</span></span><br><span class="line">  <span class="attr">enable:</span> <span class="literal">true</span></span><br><span class="line">  <span class="attr">manifest:</span> <span class="string">/img/pwa/manifest.json</span></span><br><span class="line">  <span class="attr">theme_color:</span> <span class="string">&quot;#fff&quot;</span></span><br><span class="line">  <span class="attr">apple_touch_icon:</span> <span class="string">/img/pwa/apple-touch-icon.png</span></span><br><span class="line">  <span class="attr">favicon_32_32:</span> <span class="string">/img/pwa/32.png</span></span><br><span class="line">  <span class="attr">favicon_16_16:</span> <span class="string">/img/pwa/16.png</span></span><br><span class="line">  <span class="attr">mask_icon:</span> <span class="string">/img/pwa/safari-pinned-tab.svg</span></span><br></pre></td></tr></table></figure>
<p>在 Hexo 的<code>source</code>目錄中創建<code>manifest.json</code>文件。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;string&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;short_name&quot;</span>: <span class="string">&quot;Junzhou&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;theme_color&quot;</span>: <span class="string">&quot;#49b1f5&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;background_color&quot;</span>: <span class="string">&quot;#49b1f5&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;display&quot;</span>: <span class="string">&quot;standalone&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;scope&quot;</span>: <span class="string">&quot;/&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;start_url&quot;</span>: <span class="string">&quot;/&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;icons&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/36.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;36x36&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/48.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;48x48&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/72.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;72x72&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/96.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;96x96&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/144.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;144x144&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/192.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;192x192&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;src&quot;</span>: <span class="string">&quot;images/pwaicons/512.png&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;sizes&quot;</span>: <span class="string">&quot;512x512&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;image/png&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">&quot;splash_pages&quot;</span>: <span class="literal">null</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>你也可以通過 <a target="_blank" rel="noopener" href="https://app-manifest.firebaseapp.com/">Web App Manifest</a>快速創建<code>manifest.json</code>。（Web App Manifest 要求至少包含一個 512*512 像素的圖標）</p>
<h3 id="安裝插件"><a href="#安裝插件" class="headerlink" title="安裝插件"></a>安裝插件</h3><p>在命令行中輸入安裝插件</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install workbox<span class="literal">-build</span> gulp -<span class="literal">-save</span><span class="literal">-dev</span></span><br></pre></td></tr></table></figure>
<h3 id="創建gulpfile-js-文件"><a href="#創建gulpfile-js-文件" class="headerlink" title="創建gulpfile.js 文件"></a>創建<code>gulpfile.js</code> 文件</h3><p>在 Hexo 的根目錄，創建一個<code> gulpfile.js</code>文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> gulp = <span class="built_in">require</span>(<span class="string">&quot;gulp&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> workbox = <span class="built_in">require</span>(<span class="string">&quot;workbox-build&quot;</span>);</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">&quot;generate-service-worker&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> workbox.injectManifest(&#123;</span><br><span class="line">    swSrc: <span class="string">&quot;./sw-template.js&quot;</span>,</span><br><span class="line">    swDest: <span class="string">&quot;./public/sw.js&quot;</span>,</span><br><span class="line">    globDirectory: <span class="string">&quot;./public&quot;</span>,</span><br><span class="line">    globPatterns: [<span class="string">&quot;**/*.&#123;html,css,js,json,woff2&#125;&quot;</span>],</span><br><span class="line">    modifyURLPrefix: &#123;</span><br><span class="line">      <span class="string">&quot;&quot;</span>: <span class="string">&quot;./&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">&quot;build&quot;</span>, gulp.series(<span class="string">&quot;generate-service-worker&quot;</span>));</span><br></pre></td></tr></table></figure>
<h3 id="創建-sw-template-js-文件"><a href="#創建-sw-template-js-文件" class="headerlink" title="創建 sw-template.js 文件"></a>創建 <code>sw-template.js</code> 文件</h3><p>在 Hexo 的根目錄，創建一個<code>sw-template.js</code>文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> workboxVersion = <span class="string">&quot;5.1.3&quot;</span>;</span><br><span class="line"></span><br><span class="line">importScripts(</span><br><span class="line">  <span class="string">`https://storage.googleapis.com/workbox-cdn/releases/<span class="subst">$&#123;workboxVersion&#125;</span>/workbox-sw.js`</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">workbox.core.setCacheNameDetails(&#123;</span><br><span class="line">  prefix: <span class="string">&quot;your name&quot;</span>,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">workbox.core.skipWaiting();</span><br><span class="line"></span><br><span class="line">workbox.core.clientsClaim();</span><br><span class="line"></span><br><span class="line">workbox.precaching.precacheAndRoute(self.__WB_MANIFEST, &#123;</span><br><span class="line">  directoryIndex: <span class="literal">null</span>,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">workbox.precaching.cleanupOutdatedCaches();</span><br><span class="line"></span><br><span class="line"><span class="comment">// Images</span></span><br><span class="line">workbox.routing.registerRoute(</span><br><span class="line">  /\.(?:png|jpg|jpeg|gif|bmp|webp|svg|ico)$/,</span><br><span class="line">  <span class="keyword">new</span> workbox.strategies.CacheFirst(&#123;</span><br><span class="line">    cacheName: <span class="string">&quot;images&quot;</span>,</span><br><span class="line">    plugins: [</span><br><span class="line">      <span class="keyword">new</span> workbox.expiration.ExpirationPlugin(&#123;</span><br><span class="line">        maxEntries: <span class="number">1000</span>,</span><br><span class="line">        maxAgeSeconds: <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">30</span>,</span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="keyword">new</span> workbox.cacheableResponse.CacheableResponsePlugin(&#123;</span><br><span class="line">        statuses: [<span class="number">0</span>, <span class="number">200</span>],</span><br><span class="line">      &#125;),</span><br><span class="line">    ],</span><br><span class="line">  &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Fonts</span></span><br><span class="line">workbox.routing.registerRoute(</span><br><span class="line">  /\.(?:eot|ttf|woff|woff2)$/,</span><br><span class="line">  <span class="keyword">new</span> workbox.strategies.CacheFirst(&#123;</span><br><span class="line">    cacheName: <span class="string">&quot;fonts&quot;</span>,</span><br><span class="line">    plugins: [</span><br><span class="line">      <span class="keyword">new</span> workbox.expiration.ExpirationPlugin(&#123;</span><br><span class="line">        maxEntries: <span class="number">1000</span>,</span><br><span class="line">        maxAgeSeconds: <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">30</span>,</span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="keyword">new</span> workbox.cacheableResponse.CacheableResponsePlugin(&#123;</span><br><span class="line">        statuses: [<span class="number">0</span>, <span class="number">200</span>],</span><br><span class="line">      &#125;),</span><br><span class="line">    ],</span><br><span class="line">  &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Google Fonts</span></span><br><span class="line">workbox.routing.registerRoute(</span><br><span class="line">  /^https:\/\/fonts\.googleapis\.com/,</span><br><span class="line">  <span class="keyword">new</span> workbox.strategies.StaleWhileRevalidate(&#123;</span><br><span class="line">    cacheName: <span class="string">&quot;google-fonts-stylesheets&quot;</span>,</span><br><span class="line">  &#125;)</span><br><span class="line">);</span><br><span class="line">workbox.routing.registerRoute(</span><br><span class="line">  /^https:\/\/fonts\.gstatic\.com/,</span><br><span class="line">  <span class="keyword">new</span> workbox.strategies.CacheFirst(&#123;</span><br><span class="line">    cacheName: <span class="string">&quot;google-fonts-webfonts&quot;</span>,</span><br><span class="line">    plugins: [</span><br><span class="line">      <span class="keyword">new</span> workbox.expiration.ExpirationPlugin(&#123;</span><br><span class="line">        maxEntries: <span class="number">1000</span>,</span><br><span class="line">        maxAgeSeconds: <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">30</span>,</span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="keyword">new</span> workbox.cacheableResponse.CacheableResponsePlugin(&#123;</span><br><span class="line">        statuses: [<span class="number">0</span>, <span class="number">200</span>],</span><br><span class="line">      &#125;),</span><br><span class="line">    ],</span><br><span class="line">  &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Static Libraries</span></span><br><span class="line">workbox.routing.registerRoute(</span><br><span class="line">  /^https:\/\/cdn\.jsdelivr\.net/,</span><br><span class="line">  <span class="keyword">new</span> workbox.strategies.CacheFirst(&#123;</span><br><span class="line">    cacheName: <span class="string">&quot;static-libs&quot;</span>,</span><br><span class="line">    plugins: [</span><br><span class="line">      <span class="keyword">new</span> workbox.expiration.ExpirationPlugin(&#123;</span><br><span class="line">        maxEntries: <span class="number">1000</span>,</span><br><span class="line">        maxAgeSeconds: <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">30</span>,</span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="keyword">new</span> workbox.cacheableResponse.CacheableResponsePlugin(&#123;</span><br><span class="line">        statuses: [<span class="number">0</span>, <span class="number">200</span>],</span><br><span class="line">      &#125;),</span><br><span class="line">    ],</span><br><span class="line">  &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">workbox.googleAnalytics.initialize();</span><br></pre></td></tr></table></figure>
<p>把<code>prefix</code> 修改為你博客的名字（英文），如果你想用其它緩存策略，請自行查看<a target="_blank" rel="noopener" href="https://developers.google.com/web/tools/workbox/modules/workbox-strategies">相關文檔</a></p>
<h3 id="添加-js-進主題"><a href="#添加-js-進主題" class="headerlink" title="添加 js 進主題"></a>添加 js 進主題</h3><p>在主題配置文件中,添加需要的 css 和 js</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">inject:</span></span><br><span class="line">  <span class="attr">head:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">&#x27;&lt;style type=&quot;text/css&quot;&gt;.app-refresh&#123;position:fixed;top:-2.2rem;left:0;right:0;z-index:99999;padding:0 1rem;font-size:15px;height:2.2rem;transition:all .3s ease&#125;.app-refresh-wrap&#123;display:flex;color:#fff;height:100%;align-items:center;justify-content:center&#125;.app-refresh-wrap a&#123;color:#fff;text-decoration:underline;cursor:pointer&#125;&lt;/style&gt;&#x27;</span></span><br><span class="line">  <span class="attr">bottom:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">&#x27;&lt;div class=&quot;app-refresh&quot; id=&quot;app-refresh&quot;&gt; &lt;div class=&quot;app-refresh-wrap&quot;&gt; &lt;label&gt;✨ 網站已更新最新版本 👉&lt;/label&gt; &lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;location.reload()&quot;&gt;點擊刷新&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;script&gt;function showNotification()&#123;if(GLOBAL_CONFIG.Snackbar)&#123;var t=&quot;light&quot;===document.documentElement.getAttribute(&quot;data-theme&quot;)?GLOBAL_CONFIG.Snackbar.bgLight:GLOBAL_CONFIG.Snackbar.bgDark,e=GLOBAL_CONFIG.Snackbar.position;Snackbar.show(&#123;text:&quot;已更新最新版本&quot;,backgroundColor:t,duration:5e5,pos:e,actionText:&quot;點擊刷新&quot;,actionTextColor:&quot;#fff&quot;,onActionClick:function(t)&#123;location.reload()&#125;&#125;)&#125;else&#123;var o=`top: 0; background: $&#123;&quot;light&quot;===document.documentElement.getAttribute(&quot;data-theme&quot;)?&quot;#49b1f5&quot;:&quot;#1f1f1f&quot;&#125;;`;document.getElementById(&quot;app-refresh&quot;).style.cssText=o&#125;&#125;&quot;serviceWorker&quot;in navigator&amp;&amp;(navigator.serviceWorker.controller&amp;&amp;navigator.serviceWorker.addEventListener(&quot;controllerchange&quot;,function()&#123;showNotification()&#125;),window.addEventListener(&quot;load&quot;,function()&#123;navigator.serviceWorker.register(&quot;/sw.js&quot;)&#125;));&lt;/script&gt;&#x27;</span></span><br></pre></td></tr></table></figure>
<p>上面的代碼是壓縮過的，具體相關代碼如下，可供理解。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;app-refresh&quot;</span> <span class="attr">id</span>=<span class="string">&quot;app-refresh&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;app-refresh-wrap&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span>&gt;</span>✨ 網站已更新最新版本 👉<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;javascript:void(0)&quot;</span> <span class="attr">onclick</span>=<span class="string">&quot;location.reload()&quot;</span>&gt;</span>點擊刷新<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">if</span> (<span class="string">&quot;serviceWorker&quot;</span> <span class="keyword">in</span> navigator) &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">if</span> (navigator.serviceWorker.controller) &#123;</span></span><br><span class="line"><span class="javascript">      navigator.serviceWorker.addEventListener(<span class="string">&quot;controllerchange&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line">        showNotification();</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.addEventListener(<span class="string">&quot;load&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">      navigator.serviceWorker.register(<span class="string">&quot;/sw.js&quot;</span>);</span></span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="function"><span class="keyword">function</span> <span class="title">showNotification</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">if</span> (GLOBAL_CONFIG.Snackbar) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">var</span> snackbarBg =</span></span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.documentElement.getAttribute(<span class="string">&quot;data-theme&quot;</span>) === <span class="string">&quot;light&quot;</span></span></span><br><span class="line">          ? GLOBAL_CONFIG.Snackbar.bgLight</span><br><span class="line">          : GLOBAL_CONFIG.Snackbar.bgDark;</span><br><span class="line"><span class="javascript">      <span class="keyword">var</span> snackbarPos = GLOBAL_CONFIG.Snackbar.position;</span></span><br><span class="line">      Snackbar.show(&#123;</span><br><span class="line"><span class="javascript">        text: <span class="string">&quot;已更新最新版本&quot;</span>,</span></span><br><span class="line">        backgroundColor: snackbarBg,</span><br><span class="line">        duration: 500000,</span><br><span class="line">        pos: snackbarPos,</span><br><span class="line"><span class="javascript">        actionText: <span class="string">&quot;點擊刷新&quot;</span>,</span></span><br><span class="line"><span class="javascript">        actionTextColor: <span class="string">&quot;#fff&quot;</span>,</span></span><br><span class="line"><span class="javascript">        onActionClick: <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span></span><br><span class="line">          location.reload();</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;);</span><br><span class="line"><span class="javascript">    &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">var</span> showBg =</span></span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.documentElement.getAttribute(<span class="string">&quot;data-theme&quot;</span>) === <span class="string">&quot;light&quot;</span></span></span><br><span class="line"><span class="javascript">          ? <span class="string">&quot;#49b1f5&quot;</span></span></span><br><span class="line"><span class="javascript">          : <span class="string">&quot;#1f1f1f&quot;</span>;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">var</span> cssText = <span class="string">`top: 0; background: <span class="subst">$&#123;showBg&#125;</span>;`</span>;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">document</span>.getElementById(<span class="string">&quot;app-refresh&quot;</span>).style.cssText = cssText;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span></span><br><span class="line"><span class="css">  <span class="selector-class">.app-refresh</span> &#123;</span></span><br><span class="line">    position: fixed;</span><br><span class="line">    top: -2.2rem;</span><br><span class="line">    left: 0;</span><br><span class="line">    right: 0;</span><br><span class="line">    z-index: 99999;</span><br><span class="line">    padding: 0 1rem;</span><br><span class="line">    font-size: 15px;</span><br><span class="line">    height: 2.2rem;</span><br><span class="line">    transition: all 0.3s ease;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="css">  <span class="selector-class">.app-refresh-wrap</span> &#123;</span></span><br><span class="line">    display: flex;</span><br><span class="line"><span class="css">    <span class="selector-tag">color</span>: <span class="selector-id">#fff</span>;</span></span><br><span class="line">    height: 100%;</span><br><span class="line">    align-items: center;</span><br><span class="line">    justify-content: center;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="css">  <span class="selector-class">.app-refresh-wrap</span> <span class="selector-tag">span</span> &#123;</span></span><br><span class="line"><span class="css">    <span class="selector-tag">color</span>: <span class="selector-id">#fff</span>;</span></span><br><span class="line">    text-decoration: underline;</span><br><span class="line">    cursor: pointer;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="運行-1"><a href="#運行-1" class="headerlink" title="運行"></a>運行</h3><p>在你運行<code>hexo g</code>後，記得要運行<code>gulp</code>這樣才會生效</p>
<hr>
<p>以下是結合了上面提到的 Gulp 壓縮和 PWA 的<code>gulpfile.js</code>，可供參考。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> gulp = <span class="built_in">require</span>(<span class="string">&quot;gulp&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> cleanCSS = <span class="built_in">require</span>(<span class="string">&quot;gulp-clean-css&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> htmlmin = <span class="built_in">require</span>(<span class="string">&quot;gulp-html-minifier-terser&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> htmlclean = <span class="built_in">require</span>(<span class="string">&quot;gulp-htmlclean&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> imagemin = <span class="built_in">require</span>(<span class="string">&quot;gulp-imagemin&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> workbox = <span class="built_in">require</span>(<span class="string">&quot;workbox-build&quot;</span>);</span><br><span class="line"><span class="comment">// gulp-tester (如果使用 gulp-tester,把下面的//去掉)</span></span><br><span class="line"><span class="comment">// const terser = require(&#x27;gulp-terser&#x27;);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// babel</span></span><br><span class="line"><span class="keyword">const</span> uglify = <span class="built_in">require</span>(<span class="string">&quot;gulp-uglify&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> babel = <span class="built_in">require</span>(<span class="string">&quot;gulp-babel&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//pwa</span></span><br><span class="line">gulp.task(<span class="string">&quot;generate-service-worker&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> workbox.injectManifest(&#123;</span><br><span class="line">    swSrc: <span class="string">&quot;./sw-template.js&quot;</span>,</span><br><span class="line">    swDest: <span class="string">&quot;./public/sw.js&quot;</span>,</span><br><span class="line">    globDirectory: <span class="string">&quot;./public&quot;</span>,</span><br><span class="line">    globPatterns: [<span class="string">&quot;**/*.&#123;html,css,js,json,woff2&#125;&quot;</span>],</span><br><span class="line">    modifyURLPrefix: &#123;</span><br><span class="line">      <span class="string">&quot;&quot;</span>: <span class="string">&quot;./&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//minify js babel</span></span><br><span class="line">gulp.task(<span class="string">&quot;compress&quot;</span>, <span class="function">() =&gt;</span></span><br><span class="line">  gulp</span><br><span class="line">    .src([<span class="string">&quot;./public/**/*.js&quot;</span>, <span class="string">&quot;!./public/**/*.min.js&quot;</span>])</span><br><span class="line">    .pipe(</span><br><span class="line">      babel(&#123;</span><br><span class="line">        presets: [<span class="string">&quot;@babel/preset-env&quot;</span>],</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(</span><br><span class="line">      uglify().on(<span class="string">&quot;error&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(e);</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public&quot;</span>))</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// minify js - gulp-tester (如果使用 gulp-tester,把下面前面的//去掉)</span></span><br><span class="line"><span class="comment">// gulp.task(&#x27;compress&#x27;, () =&gt;</span></span><br><span class="line"><span class="comment">//   gulp.src([&#x27;./public/**/*.js&#x27;, &#x27;!./public/**/*.min.js&#x27;])</span></span><br><span class="line"><span class="comment">//     .pipe(terser())</span></span><br><span class="line"><span class="comment">//     .pipe(gulp.dest(&#x27;./public&#x27;))</span></span><br><span class="line"><span class="comment">// )</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//css</span></span><br><span class="line">gulp.task(<span class="string">&quot;minify-css&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> gulp</span><br><span class="line">    .src(<span class="string">&quot;./public/**/*.css&quot;</span>)</span><br><span class="line">    .pipe(</span><br><span class="line">      cleanCSS(&#123;</span><br><span class="line">        compatibility: <span class="string">&quot;ie11&quot;</span>,</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public&quot;</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 壓縮 public 目錄內 html</span></span><br><span class="line">gulp.task(<span class="string">&quot;minify-html&quot;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> gulp</span><br><span class="line">    .src(<span class="string">&quot;./public/**/*.html&quot;</span>)</span><br><span class="line">    .pipe(htmlclean())</span><br><span class="line">    .pipe(</span><br><span class="line">      htmlmin(&#123;</span><br><span class="line">        removeComments: <span class="literal">true</span>, <span class="comment">//清除 HTML 註釋</span></span><br><span class="line">        collapseWhitespace: <span class="literal">true</span>, <span class="comment">//壓縮 HTML</span></span><br><span class="line">        collapseBooleanAttributes: <span class="literal">true</span>, <span class="comment">//省略布爾屬性的值 &lt;input checked=&quot;true&quot;/&gt; ==&gt; &lt;input /&gt;</span></span><br><span class="line">        removeEmptyAttributes: <span class="literal">true</span>, <span class="comment">//刪除所有空格作屬性值 &lt;input id=&quot;&quot; /&gt; ==&gt; &lt;input /&gt;</span></span><br><span class="line">        removeScriptTypeAttributes: <span class="literal">true</span>, <span class="comment">//刪除 &lt;script&gt; 的 type=&quot;text/javascript&quot;</span></span><br><span class="line">        removeStyleLinkTypeAttributes: <span class="literal">true</span>, <span class="comment">//刪除 &lt;style&gt; 和 &lt;link&gt; 的 type=&quot;text/css&quot;</span></span><br><span class="line">        minifyJS: <span class="literal">true</span>, <span class="comment">//壓縮頁面 JS</span></span><br><span class="line">        minifyCSS: <span class="literal">true</span>, <span class="comment">//壓縮頁面 CSS</span></span><br><span class="line">        minifyURLs: <span class="literal">true</span>,</span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public&quot;</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 壓縮 public/uploads 目錄內圖片</span></span><br><span class="line">gulp.task(<span class="string">&quot;minify-images&quot;</span>, <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">  gulp</span><br><span class="line">    .src(<span class="string">&quot;./public/img/**/*.*&quot;</span>)</span><br><span class="line">    .pipe(</span><br><span class="line">      imagemin(&#123;</span><br><span class="line">        optimizationLevel: <span class="number">5</span>, <span class="comment">//類型：Number  預設：3  取值範圍：0-7（優化等級）</span></span><br><span class="line">        progressive: <span class="literal">true</span>, <span class="comment">//類型：Boolean 預設：false 無失真壓縮jpg圖片</span></span><br><span class="line">        interlaced: <span class="literal">false</span>, <span class="comment">//類型：Boolean 預設：false 隔行掃描gif進行渲染</span></span><br><span class="line">        multipass: <span class="literal">false</span>, <span class="comment">//類型：Boolean 預設：false 多次優化svg直到完全優化</span></span><br><span class="line">      &#125;)</span><br><span class="line">    )</span><br><span class="line">    .pipe(gulp.dest(<span class="string">&quot;./public/img&quot;</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 執行 gulp 命令時執行的任務</span></span><br><span class="line">gulp.task(</span><br><span class="line">  <span class="string">&quot;default&quot;</span>,</span><br><span class="line">  gulp.series(</span><br><span class="line">    <span class="string">&quot;generate-service-worker&quot;</span>,</span><br><span class="line">    gulp.parallel(<span class="string">&quot;compress&quot;</span>, <span class="string">&quot;minify-html&quot;</span>, <span class="string">&quot;minify-css&quot;</span>, <span class="string">&quot;minify-images&quot;</span>)</span><br><span class="line">  )</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h2 id="Icon"><a href="#Icon" class="headerlink" title="Icon"></a>Icon</h2><p>Butterfly 主題內置了<a target="_blank" rel="noopener" href="https://fontawesome.com/">Font Awesome V5 </a>圖標，目前已更新到 5.13.0，總共有 1,588 個免費圖標。由於是國外的圖標網站，對於國內的一些網站 Icon 並不支持。如有需要，你可以引入其它的圖標服務商。</p>
<h3 id="iconfont"><a href="#iconfont" class="headerlink" title="iconfont"></a>iconfont</h3><p>國內最出名的莫過於<a target="_blank" rel="noopener" href="https://www.iconfont.cn/">iconfont</a>,功能很強大且圖標內容很豐富的矢量圖標庫。很多<code>Font Awesome</code>不支持的圖標都可以在這裏找到。同時，<a target="_blank" rel="noopener" href="https://www.iconfont.cn/">iconfont</a>支持選擇需要的圖標生成 css 鏈接，減少不必要的 CSS 加載。</p>
<h4 id="註冊賬號"><a href="#註冊賬號" class="headerlink" title="註冊賬號"></a>註冊賬號</h4><p>打開<a target="_blank" rel="noopener" href="https://www.iconfont.cn/">iconfont</a>的網站，點擊導航欄的<code>人像</code>圖標，會跳出註冊界面，按要求註冊賬號。</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont.png" alt="Snipaste_2020-05-28_21-12-01"></p>
<h4 id="添加圖標入庫"><a href="#添加圖標入庫" class="headerlink" title="添加圖標入庫"></a>添加圖標入庫</h4><p>選擇自己需要的圖標，把鼠標移到圖標上，會顯示三個按鈕（依次是添加入庫、收藏和下載），而我們需要的是把圖標添加入庫</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-add.png" alt="image-20200528205401440"></p>
<p>添加入庫後，你可以看到網站右上角<code>購物車</code>圖標顯示了<code>1</code>字，代表圖標已經添加入庫，點擊<code>購物車</code>圖標，會彈出側邊欄顯示詳情。</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-check.png" alt="image-20200528205925258"></p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-detail.png" alt="image-20200528210120442"></p>
<p>已選擇的圖標會顯示在上面，你可以重複上面的操作，把需要的圖標添加入庫，然後點擊<code>添加到項目</code>。</p>
<p>接下來會要求選擇項目名稱，沒有的自己創建一個。</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-create.png" alt="image-20200528211624459"></p>
<h4 id="生成-CSS-鏈接"><a href="#生成-CSS-鏈接" class="headerlink" title="生成 CSS 鏈接"></a>生成 CSS 鏈接</h4><p>在添加到項目之後，會跳到項目的詳情界面。點擊<code>Font class</code>，然後再點擊<code>暫無代碼，點擊生成</code>文字。網站會自動生成 CSS 鏈接，我們只需要複製鏈接就行。</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-link.png" alt="image-20200528212301786"></p>
<h4 id="添加鏈接進主題配置文件"><a href="#添加鏈接進主題配置文件" class="headerlink" title="添加鏈接進主題配置文件"></a>添加鏈接進主題配置文件</h4><p>打開<code>主題配置文件</code>，找到<code>inject</code>配置，按要求把鏈接填入</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-setting.png" alt="image-20200528212440743"></p>
<p>在我們需要使用的地方填入 icon，例如<code>Menu</code>，圖片使用格式為<code>iconfont icon名字</code></p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-setting-menu.png" alt="image-20200528213151304"></p>
<p>運行 Butterfly 之後，你就可以看到 menu 的圖標生效了</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-iconfont-show.png" alt="image-20200528213346338"></p>
<h4 id="其他添加方法"><a href="#其他添加方法" class="headerlink" title="其他添加方法"></a>其他添加方法</h4><p>除了通過引入 CSS 鏈接使用圖標，iconfont 也支持通過其它方法使用圖標，具體可查看<a target="_blank" rel="noopener" href="https://www.iconfont.cn/help/detail?helptype=code">iconfont 官方使用文檔</a></p>
<h3 id="其它圖標提供商"><a href="#其它圖標提供商" class="headerlink" title="其它圖標提供商"></a>其它圖標提供商</h3><p>除了<a target="_blank" rel="noopener" href="https://www.iconfont.cn/">iconfont</a>，還有<a target="_blank" rel="noopener" href="https://github.com/Remix-Design/RemixIcon">RemixIcon</a>、<a target="_blank" rel="noopener" href="https://www.flaticon.com/categories/seo-and-web">Flaticon</a>等等提供商，很多圖標可以選擇，具體使用方法請參考各自的文檔。</p>
<h2 id="圖片壓縮"><a href="#圖片壓縮" class="headerlink" title="圖片壓縮"></a>圖片壓縮</h2><p>Butterfly 主題需要使用到很多圖片。如果圖片太大，會嚴重拖慢網站的加載速度。</p>
<p>圖片壓縮能夠有效的緩解這個問題。</p>
<p>除了通過<code>gulp-imagemin</code>來壓縮圖片，還可以通過在綫壓縮網站和軟件來進行壓縮。以下兩款是我自己正在使用的工具。網上有很多這樣的工具，挑選一款適合自己的就行。</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="https://tinypng.com/">tinypng</a></p>
<p>一個在綫壓縮的網站。壓縮後的圖片也保留了很高的質量，在知乎上很多人推薦，不過免費版有限制。</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-tinypng.png" alt="image-20200526173511503"></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://saerasoft.com/caesium/">caesium</a></p>
<p>開源軟件，支持 Windows 和 macOS。可以批量壓縮軟件，無限制。</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-caesium.png" alt="image-20200526173316278"></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://github.com/marketplace/imgbot">imgbot</a></p>
</li>
</ul>
<p>imgbot 是一款 Github 插件。</p>
<p>安裝後，你上傳圖片到 Github 去，imgbot 會自動壓縮圖片並推送 PR，我們只需要合併 PR 就行</p>
<p>你可以配置 imgbot 的偵測方法、壓縮方法（有損/無損），具體可以查看插件的文檔</p>
<p><img src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-enhance-imgbot.png" alt="image-20200830231742951"></p>
<h2 id="插件推薦"><a href="#插件推薦" class="headerlink" title="插件推薦"></a>插件推薦</h2><ul>
<li><p><a target="_blank" rel="noopener" href="https://github.com/rozbo/hexo-abbrlink">hexo-abbrlink</a></p>
<p>可以把鏈接 permalink 轉為數字的插件，配置容易，生成時自動轉為數字。</p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo-generator-feed">hexo-generator-feed</a></p>
<p>生成 RSS 文件的插件</p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo-filter-nofollow">hexo-filter-nofollow</a></p>
<p>為網站使用到的所有外鏈添加<code>rel=&quot;noopener external nofollow noreferrer&quot;</code>, 可以有效地加強網站 SEO 和防止權重流失</p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo-generator-sitemap">hexo-generator-sitemap</a></p>
<p>生成 sitemap 的插件</p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://github.com/coneycode/hexo-generator-baidu-sitemap">hexo-generator-baidu-sitemap</a></p>
<p>看名字就知道，是專門為百度生成 sitemap 的插件</p>
</li>
</ul>
<h2 id="參考"><a href="#參考" class="headerlink" title="參考"></a>參考</h2><p><a target="_blank" rel="noopener" href="https://io-oi.me/tech/pwa-via-workbox/">利用 Workbox 實現博客的 PWA</a></p>
<p><a href="%5Bhttps://zh.wikipedia.org/wiki/%E6%B8%90%E8%BF%9B%E5%BC%8F%E7%BD%91%E7%BB%9C%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%5D(https://zh.wikipedia.org/wiki/%E6%BC%B8%E9%80%B2%E5%BC%8F%E7%B6%B2%E7%B5%A1%E6%87%89%E7%94%A8%E7%A8%8B%E5%BA%8F)">漸進式網絡應用程式</a></p>
<a class="btn-beautify button--animated block purple right larger" href="/posts/198a4240/" 
  title="✨ Butterfly 安裝文檔(七) 更新日誌"><i class="far fa-hand-point-right"></i><span>✨ Butterfly 安裝文檔(七) 更新日誌</span></a>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">娄豪杰</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/article/4073eda.html">http://example.com/article/4073eda.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">HJ BLOG</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%95%99%E7%A8%8B/">教程</a><a class="post-meta__tags" href="/tags/Hexo/">Hexo</a><a class="post-meta__tags" href="/tags/%E4%B8%BB%E9%A1%8C/">主題</a><a class="post-meta__tags" href="/tags/butterfly/">butterfly</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-06-cover.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/article/198a4240.html"><img class="prev-cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/Butterfly-docs-07-cover.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Butterfly 安裝文檔(七) 更新日誌</div></div></a></div><div class="next-post pull-right"><a href="/article/98d20436.html"><img class="next-cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-doc-05-cover.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Butterfly 安裝文檔(五) 主題問答</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/article/21cfbf15.html" title="Butterfly 安裝文檔(一) 快速開始"><img class="cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-01-cover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-01</div><div class="title">Butterfly 安裝文檔(一) 快速開始</div></div></a></div><div><a href="/article/dc584b87.html" title="Butterfly 安裝文檔(二) 主題頁面"><img class="cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/Butterfly-docs-02-cover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-02</div><div class="title">Butterfly 安裝文檔(二) 主題頁面</div></div></a></div><div><a href="/article/98d20436.html" title="Butterfly 安裝文檔(五) 主題問答"><img class="cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-doc-05-cover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-05</div><div class="title">Butterfly 安裝文檔(五) 主題問答</div></div></a></div><div><a href="/article/ceeb73f.html" title="Butterfly 安裝文檔(四) 主題配置-2"><img class="cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/Butterfly-docs-04-cover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-04</div><div class="title">Butterfly 安裝文檔(四) 主題配置-2</div></div></a></div><div><a href="/article/4aa8abbe.html" title="Butterfly 安裝文檔(三) 主題配置-1"><img class="cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-03-cover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-03</div><div class="title">Butterfly 安裝文檔(三) 主題配置-1</div></div></a></div><div><a href="/article/198a4240.html" title="Butterfly 安裝文檔(七) 更新日誌"><img class="cover" src="https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/Butterfly-docs-07-cover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-07</div><div class="title">Butterfly 安裝文檔(七) 更新日誌</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">娄豪杰</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/louhaojie99"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">人生就像品茗，懂得吃苦，才能回甘啊。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BB%BA%E8%AD%B0"><span class="toc-number">1.</span> <span class="toc-text">建議</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9F%B3%E6%A8%82"><span class="toc-number">2.</span> <span class="toc-text">音樂</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%BB%E5%BD%B1"><span class="toc-number">3.</span> <span class="toc-text">電影</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AA%AC%E8%AA%AC"><span class="toc-number">4.</span> <span class="toc-text">説説</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Artitalk"><span class="toc-number">4.1.</span> <span class="toc-text">Artitalk</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HexoPlusPlus-Talk"><span class="toc-number">4.2.</span> <span class="toc-text">HexoPlusPlus Talk</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E7%BE%A9%E4%BB%A3%E7%A2%BC%E9%85%8D%E8%89%B2"><span class="toc-number">5.</span> <span class="toc-text">自定義代碼配色</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E7%BE%A9%E5%81%B4%E9%82%8A%E6%AC%84"><span class="toc-number">6.</span> <span class="toc-text">自定義側邊欄</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E5%85%A8%E5%B1%80%E5%90%B8%E5%BA%95-Aplayer-%E6%95%99%E7%A8%8B"><span class="toc-number">7.</span> <span class="toc-text">添加全局吸底 Aplayer 教程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#jQuery-%E5%8A%A0%E8%BC%89"><span class="toc-number">8.</span> <span class="toc-text">jQuery 加載</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Gulp-%E5%A3%93%E7%B8%AE"><span class="toc-number">9.</span> <span class="toc-text">Gulp 壓縮</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%9D-Gulp"><span class="toc-number">9.1.</span> <span class="toc-text">安裝 Gulp</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E5%AE%89%E8%A3%9D"><span class="toc-number">9.2.</span> <span class="toc-text">插件安裝</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A3%93%E7%B8%AE-HTML"><span class="toc-number">9.2.1.</span> <span class="toc-text">壓縮 HTML</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A3%93%E7%B8%AE-CSS"><span class="toc-number">9.2.2.</span> <span class="toc-text">壓縮 CSS</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A3%93%E7%B8%AE-JS"><span class="toc-number">9.2.3.</span> <span class="toc-text">壓縮 JS</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A3%93%E7%B8%AE%E5%9C%96%E7%89%87"><span class="toc-number">9.2.4.</span> <span class="toc-text">壓縮圖片</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%B5%E5%BB%BA-gulpfile-%E6%96%87%E4%BB%B6"><span class="toc-number">9.3.</span> <span class="toc-text">創建 gulpfile 文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%81%8B%E8%A1%8C"><span class="toc-number">9.4.</span> <span class="toc-text">運行</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PWA"><span class="toc-number">10.</span> <span class="toc-text">PWA</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%96%8B%E5%95%9F%E8%A8%AD%E7%BD%AE%E5%92%8C%E9%85%8D%E7%BD%AE-manifest-json"><span class="toc-number">10.1.</span> <span class="toc-text">開啟設置和配置 manifest.json</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%9D%E6%8F%92%E4%BB%B6"><span class="toc-number">10.2.</span> <span class="toc-text">安裝插件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%B5%E5%BB%BAgulpfile-js-%E6%96%87%E4%BB%B6"><span class="toc-number">10.3.</span> <span class="toc-text">創建gulpfile.js 文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%B5%E5%BB%BA-sw-template-js-%E6%96%87%E4%BB%B6"><span class="toc-number">10.4.</span> <span class="toc-text">創建 sw-template.js 文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0-js-%E9%80%B2%E4%B8%BB%E9%A1%8C"><span class="toc-number">10.5.</span> <span class="toc-text">添加 js 進主題</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%81%8B%E8%A1%8C-1"><span class="toc-number">10.6.</span> <span class="toc-text">運行</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Icon"><span class="toc-number">11.</span> <span class="toc-text">Icon</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#iconfont"><span class="toc-number">11.1.</span> <span class="toc-text">iconfont</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A8%BB%E5%86%8A%E8%B3%AC%E8%99%9F"><span class="toc-number">11.1.1.</span> <span class="toc-text">註冊賬號</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E5%9C%96%E6%A8%99%E5%85%A5%E5%BA%AB"><span class="toc-number">11.1.2.</span> <span class="toc-text">添加圖標入庫</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%9F%E6%88%90-CSS-%E9%8F%88%E6%8E%A5"><span class="toc-number">11.1.3.</span> <span class="toc-text">生成 CSS 鏈接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E9%8F%88%E6%8E%A5%E9%80%B2%E4%B8%BB%E9%A1%8C%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">11.1.4.</span> <span class="toc-text">添加鏈接進主題配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E6%B7%BB%E5%8A%A0%E6%96%B9%E6%B3%95"><span class="toc-number">11.1.5.</span> <span class="toc-text">其他添加方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E5%AE%83%E5%9C%96%E6%A8%99%E6%8F%90%E4%BE%9B%E5%95%86"><span class="toc-number">11.2.</span> <span class="toc-text">其它圖標提供商</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%96%E7%89%87%E5%A3%93%E7%B8%AE"><span class="toc-number">12.</span> <span class="toc-text">圖片壓縮</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E6%8E%A8%E8%96%A6"><span class="toc-number">13.</span> <span class="toc-text">插件推薦</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%83%E8%80%83"><span class="toc-number">14.</span> <span class="toc-text">參考</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="二、JavaScript设计模式【下】"/></a><div class="content"><a class="title" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】">二、JavaScript设计模式【下】</a><time datetime="2021-09-11T16:00:00.000Z" title="发表于 2021-09-12 00:00:00">2021-09-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="一、JavaScript设计模式【上】"/></a><div class="content"><a class="title" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】">一、JavaScript设计模式【上】</a><time datetime="2021-09-10T16:00:00.000Z" title="发表于 2021-09-11 00:00:00">2021-09-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="十、Vue源码【内置组件篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】">十、Vue源码【内置组件篇】</a><time datetime="2021-09-09T16:00:00.000Z" title="发表于 2021-09-10 00:00:00">2021-09-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="九、Vue源码【指令篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】">九、Vue源码【指令篇】</a><time datetime="2021-09-08T16:00:00.000Z" title="发表于 2021-09-09 00:00:00">2021-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="八、Vue源码【过滤器篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】">八、Vue源码【过滤器篇】</a><time datetime="2021-09-07T16:00:00.000Z" title="发表于 2021-09-08 00:00:00">2021-09-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://cdn.jsdelivr.net/gh/jerryc127/CDN/img/butterfly-docs-06-cover.png')"><div id="footer-wrap"><div class="copyright">&copy;2017 - 2022 By 娄豪杰</div><div class="footer_custom_text"><div>嗨，欢迎来到我的博客，希望能帮助到您！</div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"]):not([href="/no-pjax/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener toc scroll 
  window.removeEventListener('scroll', window.tocScrollFn)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>